/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.learner.service.impl;

import cn.learner.entity.RolePermission;
import cn.learner.mapper.RolePermissionMapper;
import cn.learner.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Long> findPermissionIdByRoleId(Long roleId){
        return this.baseMapper.selectPermissionIdByRoleId(roleId);
    }
    @Override
    public Boolean deleteRolePermission(Long roleId, List<Long> permissionIds) {
        int a = 0;
        for(Long permissionId: permissionIds){
            a += this.baseMapper.deleteByPermission(roleId,permissionId);
        }
        return a == permissionIds.size();
    }

    @Override
    public Boolean insertRolePermission(Long roleId, List<Long> permissionIds) {
        int a = 0;
        for(Long permissionId: permissionIds){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permissionId);
            rolePermission.setRoleId(roleId);
            a += this.baseMapper.insert(rolePermission);
//            a += this.baseMapper.insertByRoleIdPermissionId(roleId,permissionId);
        }
        return a == permissionIds.size();
    }
}
